package com.shanghaionstar.utils;

import android.content.Context;
import android.location.Location;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.framework.utils.RxBus;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.map.core.impl.OGeoPointImpl;
import com.onstar.cn.rxconstants.RxConstants;
import com.shanghaionstar.nav.model.LocateStatus;

/**
 * Created by 6335yz on 2016/7/11.
 */
public class NavHolder {
    private LogManager log = LogManager.getInstance();
    private MyLocationUtil myLoationUtil;
    private Location myLocation = null;
    private static Context mContext;
    private LocateStatus locateStatus = LocateStatus.LOCATING;
    private String currentCity;
    private OGeoPointImpl geoPointImpl;
    private Location vehicleLocation;
    private String vehicleLocationTime;
    private String currentProvince;

    /**
     * 当前下发的tbt poi
     */
    private PoiItemInfo currentSendPoi;
    public PoiItemInfo getCurrentSendPoi() {
        return currentSendPoi;
    }

    public void setCurrentSendPoi(PoiItemInfo currentSendPoi) {
        this.currentSendPoi = currentSendPoi;
    }

    public String getVehicleLocationTime() {
        return vehicleLocationTime;
    }

    public void setVehicleLocationTime(String vehicleLocationTime) {
        this.vehicleLocationTime = vehicleLocationTime;
    }

    public String getCurrentProvince() {
        return currentProvince;
    }

    public void setCurrentProvince(String currentProvince) {
        this.currentProvince = currentProvince;
    }

    /**
     * 标示地图是否当前可见
     */
    private boolean isShowMap;

    public boolean isShowMap() {
        return isShowMap;
    }

    public void setShowMap(boolean showMap) {
        isShowMap = showMap;
    }

    public Location getVehicleLocation() {
        return vehicleLocation;
    }

    public void setVehicleLocation(Location vehicleLocation) {
        this.vehicleLocation = vehicleLocation;
    }

    public OGeoPointImpl getGeoPointImpl() {
        return geoPointImpl;
    }

    public void setGeoPointImpl(OGeoPointImpl geoPointImpl) {
        this.geoPointImpl = geoPointImpl;
    }

    public String getCurrentCity() {
        return currentCity;
    }

    public void setCurrentCity(String currentCity) {
        this.currentCity = currentCity;
    }

    public Location getMyLocation() {
        return myLocation;
    }

    public LocateStatus getLocateStatus() {
        return locateStatus;
    }

    private MyLocationUtil.LocationResult locationResult = new MyLocationUtil.LocationResult() {
        @Override
        public void gotLocation(Location location) {
            log.d("getMyLocationFromPhone()-------gotLocation location=" + location);
            if (location != null) {
                locateStatus = LocateStatus.LOCATED;
                myLocation = location;
            } else {
                locateStatus = LocateStatus.LOCATE_FAIL;
            }
            /**
             * 发送广播以通知接受者更新地理位置
             */
            RxBus.getDefault().post(RxConstants.LOCATE, locateStatus);
        }
    };


    private NavHolder(){

    }
    /**
     * context must be maintab or applicationtext
     *
     * @param context
     * @return
     */
    public static NavHolder getInstance(Context context) {
        if (mContext == null) {
            mContext = context;
        }
        return InnerHolder.navHolder;
    }

    private static class InnerHolder {
        private  static final NavHolder navHolder = new NavHolder();
    }

    public void findMyLocation() {
        if (myLoationUtil == null) {
            try {
                myLoationUtil = new MyLocationUtil(mContext, locationResult);
            } catch (Exception e) {
                locateStatus = LocateStatus.LOCATE_FAIL;
                e.printStackTrace();
                myLoationUtil = null;
            }
        }
        if (myLoationUtil != null) {
            locateStatus = LocateStatus.LOCATING;
            myLoationUtil.initMyLocation();
        }
    }
}
